package cn.xuchunh.okhttpdemo;

import android.os.Handler;
import android.os.Looper;

import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import cn.xuchunh.okhttpdemo.toolbox.MyRequest;

/**
 * Created by Administrator on 2015/12/26.
 */
public class HttpManager {

    private static HttpManager instance;
    private OkHttpClient client;

    private ResponseDelivery responseDelivery;
    private AtomicInteger idGenerator;

    public static HttpManager getInstance() {
        if (instance == null) {
            instance = new HttpManager();
        }
        return instance;
    }

    public HttpManager() {
        client = new OkHttpClient();
        responseDelivery = new ResponseDelivery(new Handler(Looper.getMainLooper()));
        idGenerator = new AtomicInteger(-1);
    }

    public void get(String url, Map<String, String> headers, Callback callback) {
        Request.Builder builder = new Request.Builder().url(url);
        if (headers != null) {
            for (String key : headers.keySet()) {
                String value = headers.get(key);
                builder.addHeader(key, value);
            }
        }
        Request request = builder.build();
        client.newCall(request).enqueue(callback);
    }

    public void get(String url, Map<String, String> headers, MyRequest.ResponseListener listener) {
        Request.Builder builder = new Request.Builder().url(url);
        if (headers != null) {
            for (String key : headers.keySet()) {
                String value = headers.get(key);
                builder.addHeader(key, value);
            }
        }
        Request request = builder.build();
        MyRequest myRequest = new MyRequest(client.newCall(request), responseDelivery, listener);
        myRequest.run();
    }

    public void get(String url, Map<String, String> headers, ResponseListener listener, Object tag) {
        Request.Builder builder = new Request.Builder().url(url);
        if (headers != null) {
            for (String key : headers.keySet()) {
                String value = headers.get(key);
                builder.addHeader(key, value);
            }
        }
        Tag myTag = new Tag();
        myTag.setId(idGenerator.incrementAndGet());
        if (tag != null) {
            myTag.setRequestTag(tag);
        }
        builder.tag(myTag);
        client.newCall(builder.build()).enqueue(new RequestCallBack());
    }

    public void post(String url, Map<String, String> headers, Callback callback, RequestBody requestBody) {
        Request.Builder builder = new Request.Builder().url(url);
        if (headers != null) {
            for (String key : headers.keySet()) {
                String value = headers.get(key);
                builder.addHeader(key, value);
            }
        }
        builder.post(requestBody);
        Request request = builder.build();
        client.newCall(request).enqueue(callback);
    }

    private class RequestCallBack implements Callback {
        @Override
        public void onFailure(Request request, IOException e) {

        }

        @Override
        public void onResponse(Response response) throws IOException {

        }
    }

}
